<?php
/**
 * Handles all calendar and event functions
 *
 *
 *  @author Jimmy E. Lewis
 *  @version 1.0
 */

class Calendar extends Controller{
	
	var $user = null;
	
	function __construct() {
		parent::__construct();
	}
	
	
	function index(){
		$this->load->view('calendar');
	}
	
	//---------------------- Function To Display Calendar ----------------------//
	
	function test(){
		$build = array('page'=>'full');
		$data = $this->_buildContent($build);
		$this->load->view('calendar/test',$data);
		
		
		
	}
	
	
	
	/**
	 * Displays a full size calendar.
	 *
	 * @param int month -- sets the display month. Defaults to current month if not set.
	 * @param int year -- sets the display year.  Defaults to current year if not set.
	 * @param String 'cal_only' -- Only displays the html that makes up the calendar.  
	 *		 In order to use this you must enter a month and year also.
	 */
	function monthly(){
			if($this->_validMonth($this->uri->segment(3))){
				$month = $this->uri->segment(3);
			}
			else{
				$month = date('n');
			}
			
			$day = 1;

			if($this->_validYear($this->uri->segment(4))){
				$year = $this->uri->segment(4);
			}
			else{
				$year = date('Y');
			}
				$daysInMonth = date("t",mktime(0,0,0,$month,1,$year));

			$start = mktime(0,0,0,$month,$day,$year);

			$dates = $this->_createCalendar($month,$day,$year);
			$a = 0;
			$b= $start;
			//get events for month
			while($a<$daysInMonth){
				$start = mktime(0,0,0,$month,$day+$a,$year);
				$end = mktime(23,59,59,$month,$day+$a,$year);	

				$events = $this->CalendarModel->getEvents($start,$end,$this->_getUserGroups());
				if($events->num_rows() > 0){
					foreach($events->result() as $row){
						$days[date("j",$start)][] = array(
														'start' => $row->start,
														'end' => $row->end,
														'name' => $row->name,
														'id' => $row->id
													);
					}


				}
				$a++;

			}
			$build = array('page'=>'full');
			$data = $this->_buildContent($build);
			
			if(isset($days)) $data = $data + array( 'events' => $days, 'dates' => $dates, 'date' => $b); //if the month has events 
			else $data = $data + array('dates' => $dates, 'date' => $b);
			
			$data['extraCSS'] = array(site_url('templates/fall2009/css/calendar.css'));
			$data['extraJS'] = array(site_url('js/jquery.tools.min.js'),site_url('js/jquery.form.js'));											
					
			$this->load->view('calendar/fullView',$data);
	}
	
	/**
	 * Displays a full size calendar with an event.
	 *
	 * @param int eventID An events id to display.
	 * @param int month -- sets the display month. Defaults to current month if not set.
	 * @param int year -- sets the display year.  Defaults to current year if not set.
	 *
	 */
	function withEvent(){
		if($this->_validMonth($this->uri->segment(4))){
			$month = $this->uri->segment(4);
		}
		else{
			$month = date('n');
		}
		
		$day = 1;

		if($this->_validYear($this->uri->segment(5))){
			$year = $this->uri->segment(5);
		}
		else{
			$year = date('Y');
		}
		
		$daysInMonth = date("t",mktime(0,0,0,$month,1,$year));
		$start = mktime(0,0,0,$month,$day,$year);
		$dates = $this->_createCalendar($month,$day,$year);
		$a = 0;
		$b= $start;

		
		while($a<$daysInMonth){ // get all the events for the month
			$start = mktime(0,0,0,$month,$day+$a,$year);
			$end = mktime(23,59,59,$month,$day+$a,$year);	

			$events = $this->CalendarModel->getEvents($start,$end,$this->_getUserGroups());
			
			if($events->num_rows() > 0){
				foreach($events->result() as $row){
					$days[date("j",$start)][] = array(
													'start' => $row->start,
													'end' => $row->end,
													'name' => $row->name,
													'id' => $row->id
												);
				}
			}
			$a++;
		}
		
		if(isset($days)) $data = array( 'events' => $days, 'dates' => $dates, 'date' => $b); // does the month have events
		else $data = array('dates' => $dates, 'date' => $b);
		($this->uri->segment(6)=='cal_only') ? $data['cal_only'] = "yes" : $data['cal_only'] = 'no'; // only replace the calendar not the page
		
		/* places event above the calendar -- for event -- */
		$data['id'] = $this->uri->segment(3);
		$event = $this->CalendarModel->getEvent($this->uri->segment(3));
		if($event!=null){
			$data += array(
							'event' => $event, 
					);
			
			$build['invited'] = $this->_isInvited($event->category);
			if($build['invited']) $data['status'] = $this->_getStatus($data['id']);
			$data['invited'] = $build['invited'];
			$build['page'] = 'full';
			$data = $data + $this->_buildContent($build);
			
			if($data['event']->geocode!=""){
				$temp = explode(',',$data['event']->geocode);
				$data['event']->geocode = $temp[0];
				$data['event']->geocode2 = $temp[1];
			}
		}
		else $data['event'] = 'no';
		/* end for event */
		
		$data['extraCSS'] = array(site_url('templates/fall2009/css/calendar.css'));
		$data['extraJS'] = array(site_url('js/jquery.tools.min.js'),site_url('js/jquery.form.js'));
		
		$this->load->view('calendar/fullView',$data);
	}
	
	//---------------------- Functions Make Calendar ----------------------//
	
	/**
	 * Creates the arrays for a weekly calendar.  Sets both the day names and numbers.
	 */
	function _createWeekly($month,$day,$year,$period){
		$start = mktime(0,0,0,$month,$day,$year);
		$a = 0;
		while($a<$period){
			$dayOrder[] = date("l",strtotime("+$a days",$start));
			$dates[] = date("Y-n-j",strtotime("+$a days",$start));
			$a++;
		}
		$data = array('dates' => $dates, 'dayOrder' => $dayOrder);
		return $data;
	}

	/**
	 * Creates the arrays for a full calendar.
	 */
	function _createCalendar($month,$day,$year){
		$date = mktime(0,0,0,$month,1,$year);
		$daysInMonth = date("t",mktime(0,0,0,$month,1,$year));
		$firstDay = date("w",mktime(0,0,0,$month,1,$year));

		$tempDays = $firstDay + $daysInMonth;
		$weeksInMonth = ceil($tempDays/7);


		$counter = 0;

		for($j=0;$j<$weeksInMonth;$j++) {
			for($i=0;$i<7;$i++) {
				$counter++;
				$week[$j][$i] = $counter;
				// offset the days
				$week[$j][$i] -= $firstDay;
				if (($week[$j][$i] < 1) || ($week[$j][$i] > $daysInMonth)) {    
					$week[$j][$i] = "";
				}
			}
		}
		return $week;
		
	}
	
	//---------------------- Functions For Items ----------------------//
	
	function assignItem(){
		$this->badger->checkAllow('admin staff xalt');
		$this->load->library('validation');
	//	$this->validation->set_rules($rules);
				
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		$id = $this->uri->segment(3);
		
		if($this->validation->run() == FALSE) {
			
			$event = $this->CalendarModel->getEvent($id);
			$data['repeating_id'] = $event->repeating_id;
			$this->load->view("calendar/assignItem/$id",$data);
		}
		else{
			$item = $this->CalendarModel->getItem($id);
			//Has this item aready been assigned?
			if($item->assign==null){
				$post['assign'] = $this->user->userid;
				$this->CalendarModel->assignItem($id,$post);
			}
			redirect("calendar/eventDetails/$item->event_id/");
		}
	}
	
	/**
	 * Displays page: This is the write page for composing an item
	 */
	function createItem(){
		$this->badger->checkAllow('admin staff xalt');
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'name';
		
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		$data['id'] = $this->uri->segment(3);
		$data['redirect'] = $this->uri->segment(4);
		if($this->validation->run() == FALSE) {
			$this->load->view('calendar/createItem',$data);
		} else {
			$post['name'] = $this->input->post('name',TRUE);
			$post['event_id'] = $data['id'];
			
			if($this->CalendarModel->itemDoesNotExist($post)) $this->CalendarModel->createItem($post);
			if($this->input->post('redirect')=='admin') redirect('admin/events');
			else redirect("calendar/eventDetails/$data[id]");
		}
	}
	
	
	/**
	 * Displays page to allow editing an item.  The editor must be an admin, xalt, of staff member
	 */
	function editItem(){
		$this->badger->checkAllow('admin staff xalt');
		$this->load->model('CalendarModel');
		$data['event'] = $this->CalendarModel->getEvent($this->uri->segment(3));
		$data = $data + $this->_buildContent();
		$this->load->view('calendar/item_edit', $data);
	
	}
	
	/**
	 * Displays page to allow deleting an item.  The deleter must be an admin, xalt, of staff member
	 */
	function deleteItem(){
		$this->badger->checkAllow('admin staff xalt');
		$this->load->model('CalendarModel');
		$this->CalendarModel->deleteItem($this->uri->segment(3));
		$data['admin'] = $this->UserModel->isUserInGroup($this->UserModel->getUserID($this->session->userdata('username')),$this->UserModel->getGroupID('admin'));
		if($data['admin']) redirect('admin/events');
		else redirect('calendar/full');
		
	}
	
	/**
	 * Displays the items for an event
	 */
	function event_items(){
		$this->badger->checkAllow('admin');
		$items = $this->CalendarModel->getItems($this->uri->segment(3));
		foreach($items->result() as $row) {
			if($row->assign!=""){
				$row->author = $this->UserModel->getUsername($row->assign);
				$row->authorname = $this->UserModel->getUser($row->assign)->display;
			}
		}
		$data['items'] = $items;
		$data['event'] = $this->uri->segment(3);
		$build = array('page'=>'eventItems');
		$data = $data + $this->_buildContent($build);
		$data['pagetype'] = 'page_admin';
		$data['admin_nav'] = TRUE;
		$this->load->view('calendar/eventItems', $data);
	}
	
	//---------------------- Functions For Events ----------------------//
	
	
	/**
	 * Displays page: This is the write page for composing a event
	 */
	function create(){
		$this->badger->checkAllow('admin staff xalt');
		$build = array('page'=>'createEvent');
		$data = $this->_buildContent($build);
		$userGroups = $this->UserModel->getUserAssigns($data['con_user']->userid);
		$category['XA'] = 'Chi Alpha';
		if($userGroups!=false){
			foreach($userGroups->result() as $userGroup){
				$category["$userGroup->groupid"] = $userGroup->groupname;
			}
		}
		$data['category'] = $category;
		
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['description'] = "trim|required|required";
		
		$times = $this->input->post('startTime') . "," . $this->input->post('endDay') . "," . $this->input->post('endTime');
		
		$rules['building'] = "trim|required";
		$rules['where'] = "trim";
		$rules['startDay'] = "trim|required";
		$rules['endDay'] = "trim|required|callback__date_check[$times]";
		$rules['status'] = "callback__status_check";
		
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'name';
		$fields['description']	= 'description';
		$fields['repeating']	= 'repeating';
		$fields['repeating_end'] = 'repeating_end';
		$fields['repeat_when'] = 'repeat_when';
		$fields['startDay']	= 'startDay';
		$fields['startTime']	= 'startTime';
		$fields['endDay']	= 'endDay';
		$fields['endTime']	= 'endTime';
		$fields['where'] = 'where';
		$fields['building'] = 'building';
		$fields['street'] = 'street';
		$fields['city'] = 'city';
		$fields['state'] = 'state';
		$fields['homepage'] = 'homepage';
		$fields['category'] = 'category';
		$fields['geocode_point'] = 'geocode_point';
		$fields['status'] = 'status';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$data['extraCSS'] = array(site_url('templates/fall2009/css/datepicker.css'),site_url('templates/fall2009/css/calendar.css'),site_url('templates/fall2009/css/timepicker.css'));
			$data['extraJS'] = array(site_url('js/date.js'),site_url('js/jquery.datePicker.js'),site_url('js/jquery.tools.min.js'),site_url('js/jquery.timePicker.js'),site_url('js/calendar.js'));
			
			$this->load->view('calendar/createEvent',$data);
			
		}
		else {
			$repeat_when = $this->input->post('repeat_when');
			if($this->input->post('aday')=='all_day') $allDay = true;
			else $allDay = false;
			
			$startTime = ''; $endTime = '';
			
			if($this->input->post('startTime')!='') $startTime = date("G:i:s",strtotime($this->input->post('startTime')));
			if($this->input->post('endTime')!='') $endTime = date("G:i:s",strtotime($this->input->post('endTime')));
			
			$post['name'] = $this->input->post('name');
			$post['description'] = $this->input->post('description');
			$post['category'] = $this->input->post('category');
			$post['homepage'] = $this->input->post('homepage');
			$post['creatorid'] = $this->user->userid;

			if($this->input->post('status')=='200'){
				$post['street'] = $this->input->post('street');
				$post['city'] = $this->input->post('city');
				$post['state'] = $this->input->post('state');
				$post['geocode'] = $this->input->post('geocode_point');
			}
			if($this->input->post('building')!='') $post['building'] = $this->input->post('building');
			($allDay) ? $post['all_day'] = '1' : $post['all_day'] = '0';
			
			if($repeat_when==0){ // not a repeating event
				if($allDay){ //an all day event
					$post['start'] = date("Y-m-d",strtotime($this->input->post('startDay')));
					$post['end'] = date("Y-m-d",strtotime($this->input->post('endDay')));
				}
				else{ //not and all day event
					$post['start'] = date("Y-m-d G:i:s",strtotime($this->input->post('startDay') . " " . $this->input->post('startTime')));
					$post['end'] = date("Y-m-d G:i:s",strtotime($this->input->post('endDay') . " " . $this->input->post('endTime')));
				}	
				$this->CalendarModel->createEvent($post);
			}
			else if($repeat_when==1){ //repeating daily
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				
				if($this->input->post('startTime')!='') $startTime = date("G:i:s",strtotime($this->input->post('startTime')));
				if($this->input->post('endTime')!='') $endTime = date("G:i:s",strtotime($this->input->post('endTime')));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_frequency'] = $this->input->post('repeat_days');
				$post['repeating_end'] = date("Y-m-d",$end);
				while($startDay<=$end){
					$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
					$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
					$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] day",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] day",$endDay)));
					$this->CalendarModel->createEvent($post);
				}
			}
			else if($repeat_when==2){ //repeating weekdays
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_end'] = date("Y-m-d",$end);
				while($startDay<=$end){
					if(date("w",$startDay)!=0 && date("w",$startDay)!=6){
						$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
						$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
						$this->CalendarModel->createEvent($post);
					}
					$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
					
				}
			}
			else if($repeat_when==3){ //repeating monday wednesday friday
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_end'] = date("Y-m-d",$end);
				while($startDay<=$end){
					if(date("w",$startDay)==1 || date("w",$startDay)==3 || date("w",$startDay)==5){
						$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
						$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
						$this->CalendarModel->createEvent($post);
					}
					$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
					
				}
			}
			else if($repeat_when==4){ //repeating tuesday thursday
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_end'] = date("Y-m-d",$end);
				while($startDay<=$end){
					if(date("w",$startDay)==2 || date("w",$startDay)==4){
						$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
						$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
						$this->CalendarModel->createEvent($post);
					}
					$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
					
				}
			}
			else if($repeat_when==5){ //repeating weekly
				$end = strtotime($this->input->post('end_repeating_date'));
				$days = $this->input->post('days_of_week');
				
				$startDay = strtotime($this->input->post('startDay'));
				$endDay = strtotime($this->input->post('endDay'));
				
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_frequency'] = $this->input->post('repeat_weeks');
				$post['repeating_days'] = implode(",",$days);
				$post['repeating_end'] = date("Y-m-d",$end);
				while($startDay<=$end){
					for($i=0;$i<sizeof($days);$i++){
						$post['start'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$startDay) . " $startTime")));
						$post['end'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$endDay) . " $endTime")));
						$this->CalendarModel->createEvent($post);
					}
					$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$endDay)));
					
				}
			}
			else if($repeat_when==6){ //repeating monthly
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				$startTime = date("G:i:s",strtotime($this->input->post('startTime')));
				$endTime = date("G:i:s",strtotime($this->input->post('endTime')));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_frequency'] = $this->input->post('repeat_months');
				$post['repeating_end'] = date("Y-m-d",$end);
				if($this->input->post('repeat_how')=='day_of_the_month'){ //repeat monthly on a date i.e. on the 12th
					while($startDay<=$end){
						$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
						$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
						$this->CalendarModel->createEvent($post);
					
						$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$startDay)));
						$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$endDay)));
					
					}
				}
				else{ //repeat on a day of the week each month i.e. the second thursday
					$firstDayOfMonth = date("w",strtotime(date("F",$startDay) . "1"));
					$strDay = date("w",$startDay);
					if($strDay>=$firstDayOfMonth) $frequency = floor(date("j",$startDay)/7+1);
					else $frequency = ceil(date("j",$startDay)/7);
					$startMonth = mktime(0,0,0,date("n",$startDay),0,date("Y",$startDay));
					$endMonth = mktime(0,0,0,date("n",$endDay),0,date("Y",$endDay));
					while($startDay<=$end){
						$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
						$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
						$this->CalendarModel->createEvent($post);
						
						$startMonth = strtotime("+1 month",$startMonth);
						$endMonth = strtotime("+1 month",$endMonth);
						echo $frequency . date("l",$startDay) . "<br/>";
						$startDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$startDay),$startMonth)));
						$endDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$endDay),$endMonth)));
					
					}
					
				}
			}
			else if($repeat_when==7){ //repeating yearly
				$end = strtotime($this->input->post('end_repeating_date'));
				$startDay = strtotime($this->input->post('startDay'));
				$startTime = date("G:i:s",strtotime($this->input->post('startTime')));
				$endTime = date("G:i:s",strtotime($this->input->post('endTime')));
				$endDay = strtotime($this->input->post('endDay'));
				$post['repeating_id'] = str_replace(' ','',substr($this->input->post('name'),0,10) . time());
				$post['repeating_interval'] = $repeat_when;
				$post['repeating_frequency'] = $this->input->post('repeat_years');
				$post['repeating_end'] = date("Y-m-d",$end);
				
				while($startDay<=$end){
					$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
					$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
					$this->CalendarModel->createEvent($post);
				
					$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$startDay)));
					$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$endDay)));
				
				}
			}
			redirect('calendar');
		}
		
	}
	/**
	 * 
	 * @param $times Array with: 0=>startTime, 1=>endDay, 2=>endTime
	 */
	function _date_check($startDay,$times){
		$times = explode(",",$times);
		$start = strtotime($startDay . ' ' . $times[0]);
		$end = strtotime($times[1] . ' ' . $times[2]);
		if($end>=$start){
			return true;
		}
		else{
//			echo $times;
			
			$this->validation->set_message('_date_check','Invalid time entered.  Please recheck the when fields.');
			return false;
		}
	}
	
	function _status_check($status){
			if($status=='200'){
				return true;
			}
			else{
				$this->validation->set_message('_status_check', 'Google Maps was unable to understand your address');
				return false;
			}
	}
	
	function deleteEvent(){
		$this->badger->checkAllow('admin staff xalt');
		$rules['id'] = "trim";
		$rules['redirect'] = "trim";
		$this->load->library('validation');
		$this->validation->set_rules($rules);
		
		$fields['id'] = 'id';
		$fields['redirect'] = 'redirect';
		
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		if($this->validation->run() == FALSE) {
			
			$data['redirect'] = ($this->validation->redirect != NULL) ? $this->validation->id : $this->uri->segment(4);
			$data['id'] = ($this->validation->id != NULL) ? $this->validation->id : $this->uri->segment(3);
			$event = $this->CalendarModel->getEvent($data['id']);
			$data['repeating_id'] = $event->repeating_id;
			$this->load->view('calendar/deleteEvent',$data);
		}
		else{
			$redirect = $this->input->post('redirect');
			$id = $this->input->post('id');
			if($redirect=='admin') $redirect = 'admin';
			else $redirect = 'withEvent';
			if($this->input->post('submit')!='Cancel'){
				$delete = $this->input->post('submit');
				
				if($delete=='Yes'){
					$this->CalendarModel->deleteEvent($id); //not a repeating event so just delete
					if($redirect=='admin') redirect("admin/events");
					else redirect("calendar");
				}
				else if($delete=='Delete This Only'){
					$this->CalendarModel->deleteEvent($id); //delete only this event
					if($redirect=='admin') redirect("admin/events");
					else redirect('calendar');
				}
				else{ // delete all repeating events for this event
					$event = $this->CalendarModel->getEvent($id);
	
					$this->CalendarModel->deleteRepeatingEvent($event->repeating_id);
					if($redirect=='admin') redirect("admin/events");
					else redirect('calendar');
				}
			}
			else{
				if($redirect=='admin') redirect("admin/events");
				else redirect("calendar/$redirect/$id");
			}
		}

	}
	
	function editEvent(){
		$this->badger->checkAllow('admin staff xalt');
		$rules['id'] = "trim";
		$rules['redirect'] = "trim";
		$this->load->library('validation');
		$this->validation->set_rules($rules);
		
		$fields['id'] = 'id';
		$fields['redirect'] = 'redirect';
		
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		if($this->validation->run() == FALSE) {
			
			$data['id'] = ($this->validation->id != NULL) ? $this->validation->id : $this->uri->segment(3);
			$data['redirect'] = ($this->validation->redirect != NULL) ? $this->validation->id : $this->uri->segment(4);
			$event = $this->CalendarModel->getEvent($data['id']);
			$data['repeating_id'] = $event->repeating_id;
			$this->load->view('calendar/editEvent',$data);
		}
		else{
			$id = $this->input->post('id');
			if($this->input->post('submit')!='Cancel'){
				$edit = strtolower($this->input->post('submit'));
				$edit = str_replace(' ', '', $edit);
				redirect("calendar/doEditEvent/$id/$edit");
			}
			else{
				if($redirect=='admin') redirect("admin/events");
				else redirect("calendar/$redirect/$id");
			}
		}
		
	}
	
	function _getOldEvents($start,$repeatingId){
		$events = $this->CalendarModel->getRepeatingEventsFrom($start,$repeatingId);
		
		$oldEvents = null;
		foreach($events->result() as $event){
			$oldEvents[date("Y-m-d",strtotime($event->start))] = $event;
		}
		return $oldEvents;
	}
	
	
	/**
	 * Displays page to allow editing an event.  The editor must be an admin, xalt, of staff member
	 */
	function doEditEvent(){
		$this->badger->checkAllow('admin staff xalt');
		$build = array('page'=>'createEvent');
		$data = $this->_buildContent($build);
		$userGroups = $this->UserModel->getUserAssigns($data['con_user']->userid);
		$category['XA'] = 'Chi Alpha';
		if($userGroups!=false){
			foreach($userGroups->result() as $userGroup){
				$category["$userGroup->groupid"] = $userGroup->groupname;
			}
		}
		$data['category'] = $category;
		
		$this->load->library('validation');
		
		$rules['name'] = "trim|required";
		$rules['description'] = "trim|required|required";
		
		$times = $this->input->post('startTime') . "," . $this->input->post('endDay') . "," . $this->input->post('endTime');
		
		$rules['building'] = "trim|required";
		$rules['where'] = "trim";
		$rules['startDay'] = "trim|required";
		$rules['endDay'] = "trim|required|callback__date_check[$times]";
		$rules['status'] = "callback__status_check";
		
		$this->validation->set_rules($rules);
		
		
		$fields['name']	= 'name';
		$fields['description']	= 'description';
		$fields['repeat_when']	= 'repeat_when';
		$fields['repeating_end'] = 'repeating_end';
		$fields['startDay']	= 'startDay';
		$fields['startTime']	= 'startTime';
		$fields['endDay']	= 'endDay';
		$fields['endTime']	= 'endTime';
		$fields['end_repeating_date'] = 'end_repeating_date';
		$fields['where'] = 'where';
		$fields['building'] = 'building';
		$fields['street'] = 'street';
		$fields['city'] = 'city';
		$fields['state'] = 'state';
		$fields['homepage'] = 'homepage';
		$fields['category'] = 'category';
		$fields['geocode_point'] = 'geocode_point';
		$fields['status'] = 'status';
		$this->validation->set_fields($fields);
	
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		$event->id = $this->uri->segment('3');
		$edit = $this->uri->segment('4');
		$data['edit'] = $edit;
		if($this->validation->run() == FALSE) {
			$eventHold = $this->CalendarModel->getEvent($event->id);
			$event->name = ($this->validation->name != NULL) ? $this->validation->name : $eventHold->name;
			$event->description = ($this->validation->name != NULL) ? $this->validation->description : $eventHold->description;
			$event->repeat_when = ($this->validation->repeat_when != NULL) ? $this->validation->repeat_when : $eventHold->repeating_interval;
			$event->startDay = ($this->validation->startDay != NULL) ? $this->validation->startDay : date("m/d/Y",strtotime(substr($eventHold->start,0,10)));
			$event->startTime = ($this->validation->startTime != NULL) ? $this->validation->startTime : (substr($eventHold->start,11,5) != "00:00") ? date("h:i A",strtotime(substr($eventHold->start,11,5))) : "";
			$event->endDay = ($this->validation->endDay != NULL) ? $this->validation->endDay : date("m/d/Y",strtotime(substr($eventHold->start,0,10)));
			$event->endTime = ($this->validation->endTime != NULL) ? $this->validation->endTime : (substr($eventHold->end,11,5) != "00:00") ? date("h:i A",strtotime(substr($eventHold->end,11,5))) : "";
			$event->repeatingEnd = ($this->validation->end_repeating_date != NULL) ? $this->validation->end_repeating_date : date('m/d/Y',strtotime($eventHold->repeating_end));
			$event->where = ($this->validation->where != NULL) ? $this->validation->where : ($eventHold->street!="" && $eventHold->city!="" && $eventHold->state!="") ? "$eventHold->street $eventHold->city, $eventHold->state" : "";
			$event->building = ($this->validation->building != NULL) ? $this->validation->building : $eventHold->building;
			$event->street = ($this->validation->street != NULL) ? $this->validation->street : $eventHold->street;
			$event->city = ($this->validation->city != NULL) ? $this->validation->city : $eventHold->city;
			$event->state = ($this->validation->state != NULL) ? $this->validation->state : $eventHold->state;
			$event->homepage = ($this->validation->homepage != NULL) ? $this->validation->homepage : $eventHold->homepage;
			$event->category = ($this->validation->category != NULL) ? $this->validation->category : $eventHold->category;
			$event->geocode_point = ($this->validation->geocode_point != NULL) ? $this->validation->geocode_point : $eventHold->geocode;
			$event->status = ($this->validation->status != NULL) ? $this->validation->status : "";
			$data['event'] = $event;
			$this->load->view('calendar/doEditEvent',$data);
		}
		else{
			$event = $this->CalendarModel->getEvent($event->id);
			$repeat_when = $this->input->post('repeat_when');
			$post['repeating_interval'] = $repeat_when;
			$post['name'] = $this->input->post('name');
			$post['description'] = $this->input->post('description');
			$post['category'] = $this->input->post('category');
			$post['homepage'] = $this->input->post('homepage');
			if($this->input->post('aday')=='all_day') $allDay = true;
			else $allDay = false;
			if($this->input->post('status')=='200'){
				$post['street'] = $this->input->post('street');
				$post['city'] = $this->input->post('city');
				$post['state'] = $this->input->post('state');
				$post['geocode'] = $this->input->post('geocode_point');
			}
			if($this->input->post('building')!='') $post['building'] = $this->input->post('building');
			($allDay) ? $post['all_day'] = '1' : $post['all_day'] = '0';
			
			$startTime = ''; $endTime = '';

			if($this->input->post('startTime')!='') $startTime = date("G:i:s",strtotime($this->input->post('startTime')));
			if($this->input->post('endTime')!='') $endTime = date("G:i:s",strtotime($this->input->post('endTime')));
			$post['start'] = date("Y-m-d G:i:s",strtotime($this->input->post('startDay') . " $startTime"));
			$post['end'] = date("Y-m-d G:i:s",strtotime($this->input->post('endDay') . " $endTime" ));

			if($edit=="onlythisinstance"){
				$post['repeating_interval'] = $event->repeating_interval;
				$this->CalendarModel->updateEvent($event->id,$post);
			}
			elseif($event->repeating_interval==0){
					switch($repeat_when){ // user wants event to repeat this way
						case 0: // event is now a non-repeating event
							$this->CalendarModel->updateEvent($event->id,$post);
							break;
						case 1: // event now repeats daily
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = $this->input->post('repeat_days');
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+$post[repeating_frequency] day",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+$post[repeating_frequency] day",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] day",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] day",$endDay)));
								$this->CalendarModel->createEvent($post);
							}
							break;
						case 2: // event now repeats on weekdays
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = null;
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+1 day",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+1 day",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								if(date("w",$startDay)!=0 && date("w",$startDay)!=6){
									$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
									$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
									$this->CalendarModel->createEvent($post);
								}
								$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));

							}
							break;
						case 3: // event now repeat MWF
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = null;
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+1 day",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+1 day",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								if(date("w",$startDay)==1 || date("w",$startDay)==3 || date("w",$startDay)==5){
									$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
									$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
									$this->CalendarModel->createEvent($post);
								}
								$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));

							}
							break;
						case 4: // event now repeats TTh
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = null;
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+1 day",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+1 day",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								if(date("w",$startDay)==2 || date("w",$startDay)==4){
									$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
									$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
									$this->CalendarModel->createEvent($post);
								}
								$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));

							}
							break;
						case 5: // event now repeats weekly
							$end = strtotime($this->input->post('end_repeating_date'));
							$days = $this->input->post('days_of_week');
							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = $this->input->post('repeat_weeks');
							$post['repeating_days'] = implode(",",$days);
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+$post[repeating_frequency] week",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+$post[repeating_frequency] week",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								for($i=0;$i<sizeof($days);$i++){
									$post['start'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$startDay) . " $startTime")));
									$post['end'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$endDay) . " $endTime")));
									$this->CalendarModel->createEvent($post);
								}
								$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$endDay)));

							}
							break;
						case 6: // event now repeats monthly
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = $this->input->post('repeat_months');
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							if($this->input->post('repeat_how')=='day_of_the_month'){
								$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$endDay)));

								while($startDay<=$end){
									$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
									$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
									$this->CalendarModel->createEvent($post);

									$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$startDay)));
									$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$endDay)));

								}
							}
							else{
								$firstDayOfMonth = date("w",strtotime(date("F",$startDay) . "1"));
								$strDay = date("w",$startDay);
								if($strDay>=$firstDayOfMonth) $frequency = floor(date("j",$startDay)/7+1);
								else $frequency = ceil(date("j",$startDay)/7);
								$startMonth = mktime(0,0,0,date("n",$startDay),0,date("Y",$startDay));
								$endMonth = mktime(0,0,0,date("n",$endDay),0,date("Y",$endDay));

								$startMonth = strtotime("+1 month",$startMonth);
								$endMonth = strtotime("+1 month",$endMonth);
								$startDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$startDay),$startMonth)));
								$endDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$endDay),$endMonth)));

								while($startDay<=$end){
									$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
									$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
									$this->CalendarModel->createEvent($post);

									$startMonth = strtotime("+1 month",$startMonth);
									$endMonth = strtotime("+1 month",$endMonth);
									$startDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$startDay),$startMonth)));
									$endDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$endDay),$endMonth)));
								}
							}
							break;
						case 7: // event now repeats yearly
							$end = strtotime($this->input->post('end_repeating_date'));

							$post['repeating_id'] = substr($this->input->post('name'),0,10) . time();
							$post['repeating_interval'] = $repeat_when;
							$post['repeating_frequency'] = $this->input->post('repeat_years');
							$post['repeating_end'] = date("Y-m-d",$end);

							$this->CalendarModel->updateEvent($event->id,$post);

							$startDay = strtotime("+$post[repeating_frequency] year",strtotime($this->input->post('startDay')));
							$endDay = strtotime("+$post[repeating_frequency] year",strtotime($this->input->post('endDay')));

							while($startDay<=$end){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$this->CalendarModel->createEvent($post);

								$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$endDay)));

							}
							break;
					}
			}
			else{
				$end = strtotime($this->input->post('end_repeating_date'));
				switch($repeat_when){
					case 0:
						$repeating_id = $event->repeating_id;
						$post['repeating_id'] = 'no';
						$post['repeating_interval'] = 0;
						$post['repeating_frequency'] = null;
						$post['repeating_end'] = null;
						$post['repeating_days'] = null;
						
						$this->CalendarModel->updateEvent($event->id,$post);

						if($edit=="allfollowing") $this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($this->input->post("startDay")))) . " 00:00:00",$repeating_id);
						elseif($edit=="alleventsintheseries") $this->CalendarModel->deleteRepeatingEvent($repeating_id);
					break;
					case 1:
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
							$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
							$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
							$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
							
							$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 2: // event now repeats on weekdays
						$post['repeating_id'] = $event->repeating_id;
						$post['repeating_interval'] = $repeat_when;
						$post['repeating_frequency'] = null;
						$post['repeating_end'] = date("Y-m-d",$end);
						
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							
							if(date("w",$startDay)!=0 && date("w",$startDay)!=6){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
							}
							$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 3: // event now repeat MWF
						$post['repeating_id'] = $event->repeating_id;
						$post['repeating_interval'] = $repeat_when;
						$post['repeating_frequency'] = null;
						$post['repeating_end'] = date("Y-m-d",$end);
						
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							
							if(date("w",$startDay)==1 || date("w",$startDay)==3 || date("w",$startDay)==5){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
							}
							$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 4: // event now repeats TTh
						$post['repeating_id'] = $event->repeating_id;
						$post['repeating_interval'] = $repeat_when;
						$post['repeating_frequency'] = null;
						$post['repeating_end'] = date("Y-m-d",$end);
						
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							
							if(date("w",$startDay)==2 || date("w",$startDay)==4){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
							}
							$startDay = strtotime(date("Y-m-d",strtotime("+1 day",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+1 day",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 5: // event now repeats weekly
						$post['repeating_id'] = $event->repeating_id;
						$post['repeating_interval'] = $repeat_when;
						$post['repeating_frequency'] = $this->input->post('repeat_weeks');
						$post['repeating_end'] = date("Y-m-d",$end);
						
						$days = $this->input->post('days_of_week');
						
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							for($i=0;$i<sizeof($days);$i++){
								$post['start'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$startDay) . " $startTime")));
								$post['end'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$endDay) . " $endTime")));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
							}
							$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] week",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 6: // event now repeats monthly
						$post['repeating_id'] = $event->repeating_id;
						$post['repeating_interval'] = $repeat_when;
						$post['repeating_frequency'] = $this->input->post('repeat_months');
						$post['repeating_end'] = date("Y-m-d",$end);
						
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						
						if($this->input->post('repeat_how')=='day_of_the_month'){ //repeat monthly on a date i.e. on the 12th
							while($startDay<=$end){
								$post['start'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$startDay) . " $startTime")));
								$post['end'] = date("Y-m-d G:i:s",strtotime($days[$i],strtotime(date("Y-m-d",$endDay) . " $endTime")));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;							
								$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$startDay)));
								$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] month",$endDay)));
							}
						}
						else{ //repeat on a day of the week each month i.e. the second thursday
							$firstDayOfMonth = date("w",strtotime(date("F",$startDay) . "1"));
							$strDay = date("w",$startDay);
							if($strDay>=$firstDayOfMonth) $frequency = floor(date("j",$startDay)/7+1);
							else $frequency = ceil(date("j",$startDay)/7);
							$startMonth = mktime(0,0,0,date("n",$startDay),0,date("Y",$startDay));
							$endMonth = mktime(0,0,0,date("n",$endDay),0,date("Y",$endDay));
							while($startDay<=$end){
								$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
								$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
								$eventInfo = array('start'=>$post['start'],'end'=>$post['end']);
								$newEvents[date("Y-m-d",strtotime($post['start']))] = $eventInfo;
								$startMonth = strtotime("+1 month",$startMonth);
								$endMonth = strtotime("+1 month",$endMonth);
								$startDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$startDay),$startMonth)));
								$endDay = strtotime(date("Y-m-d",strtotime("$frequency " . date("l",$endDay),$endMonth)));
							}
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;
					case 7: // event now repeats yearly
						$newStartDay = strtotime($this->input->post("startDay"));
						$startDay = $newStartDay;
						$endDay = strtotime($this->input->post('endDay'));
						$oldEvents = $this->_getOldEvents($newStartDay,$post['repeating_id']);
						
						/**
						 * Create array of dates based on the how the event is now repeating
						 */
						while($startDay<=$end){
							
							$post['start'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$startDay) . " $startTime"));
							$post['end'] = date("Y-m-d G:i:s",strtotime(date("Y-m-d",$endDay) . " $endTime"));
							$this->CalendarModel->createEvent($post);

							$startDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$startDay)));
							$endDay = strtotime(date("Y-m-d",strtotime("+$post[repeating_frequency] year",$endDay)));
						}
						
						$post['start'] = null;$post['end']=null;
						
						/**
						 * Go through and see if an event was already repeating on this date; if so just update the event with the new
						 * information.
						 */
						foreach($newEvents as $newEvent){
							if(array_key_exists(substr($newEvent['start'],0,10),$oldEvents)){
								$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
								$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
								$this->CalendarModel->updateEvent($oldEvents[substr($newEvent['start'],0,10)]->id,$post);
								unset($oldEvents[substr($newEvent['start'],0,10)]);
								unset($newEvents[substr($newEvent['start'],0,10)]);
							}
						}
						
						/**
						 * Delete events that are no longer valid on the new repeating style.
						 */
						foreach($oldEvents as $oldEvent){
							$this->CalendarModel->deleteEvent($oldEvent->id);
						}
						
						/**
						 * Create events for the days that weren't there already.
						 */
						foreach($newEvents as $newEvent){
							$post['start'] =  $oldEvents[substr($newEvent['start'],0,10)]->start;
							$post['end'] = 	$oldEvents[substr($newEvent['start'],0,10)]->end;
							$this->CalendarModel->createEvent($post);
						}
					
						$this->CalendarModel->deleteRepeatingFrom(date("Y-m-d",strtotime("+1 day",strtotime($end))) . " 00:00:00",$post['repeating_id']);
					break;	
					
					
					
					
				}
			}

			redirect('calendar');
		}
	}


	/*
	 * Displays and event
	 */
	function event($id){
		//ADD VALIDATION CHECK FOR USER
		$event = $this->CalendarModel->getEvent($id);
		if($event!=null){
			$data = array(
							'event' => $event, 
					);
			$build['page'] = 'eventDetails';
			$build['id'] = $id;
			$data = $data + $this->_buildContent($build);
		
			$data['invited'] = false;
			if(isset($data['con_user'])){
				$userGroups = $this->_getUserGroups();
				foreach($userGroups as $group){
					if($group==$event->category) $data['invited'] = true;
				}
				if($event->category=='XA') $data['invited'] = true;
			}
			if($data['invited']==true){
				$info = array('userid'=>$data['con_user']->userid, 'eventid'=>$id);
				$data['status'] = $this->CalendarModel->getRSVP($info);
				if($data['status']!=null){
					if($data['status']->answer=='yes') $data['status'] = 'attending this event.';
					elseif($data['status']->answer=='no') $data['status'] = 'not attending this event.';
					else $data['status'] = 'maybe attending this event.';
				}
				else $data['status'] = 'not attending this event.';
			}
			if($data['event']->geocode!=""){
				$temp = explode(',',$data['event']->geocode);
				$data['event']->geocode = $temp[0];
				$data['event']->geocode2 = $temp[1];
			}
			$this->load->view('calendar/eventView',$data);
		}
		else return "";
	}
	
	function _isInvited($eventCategory){
		if($this->user!=null){
			$userGroups = $this->_getUserGroups();
			if($userGroups!=false){
				foreach($userGroups->result() as $group){
					if($group->groupid==$eventCategory) return true;
				}
			}
			if($eventCategory=='XA') return true;
		}
		else return false;
	}
	/**
	 * @param $id An Event id.
	 */
	function _getStatus($id){
			$info = array('userid'=>$this->user->userid, 'eventid'=>$id);
			$status = $this->CalendarModel->getRSVP($info);
			if($status!=null){
				if($status->answer=='yes') return 'attending this event.';
				elseif($status->answer=='no') return 'not attending this event.';
				else return 'maybe attending this event.';
			}
			else return 'not attending this event.';
	}
	
	function _getUserGroups(){
		if($this->user!=null) return $this->UserModel->getUserAssigns($this->user->userid);
		else return 'xa';
	}
	
	function eventDetails($id){
		$event = $this->CalendarModel->getEvent($id);
		if($event!=null){ //Is this event a valid event
			$items = $this->CalendarModel->getItems($id);
			$info = array('userid'=>null, 'eventid'=>$id);
			$coming = $this->CalendarModel->getRSVP($info,'yes');
			$notComing = $this->CalendarModel->getRSVP($info,'no');
			$maybeComing = $this->CalendarModel->getRSVP($info,'maybe');
			foreach($items->result() as $row){
				if($row->assign!=null) $row->user = "- " . $this->UserModel->getDisplayName($row->assign);
				else $row->user = "";
			}
			
			foreach ($coming->result() as $row) {
				$row->author = $this->UserModel->getDisplayName($row->user_id);
			}
			foreach ($notComing->result() as $row) {
				$row->author = $this->UserModel->getDisplayName($row->user_id);
			}
			foreach ($maybeComing->result() as $row) {
				$row->author = $this->UserModel->getDisplayName($row->user_id);
			}

			$data = array(
							'event' => $event,
							'items' => $items,
							'coming' => $coming,
							'notComing' => $notComing,
							'maybeComing' => $maybeComing
					);
		
			$build['id'] = $id;
			$build['page'] = 'eventDetails';
			$build['invited'] = $this->_isInvited($event->category);
			if($build['invited']) $data['status'] = $this->_getStatus($id);
			$data['invited'] = $build['invited'];
			
			$data = $data + $this->_buildContent($build);
			
			if($data['event']->geocode!=""){
				$temp = explode(',',$data['event']->geocode);
				$data['event']->geocode = $temp[0];
				$data['event']->geocode2 = $temp[1];
			}
			$data['extraHead'] = '<link rel="stylesheet" href="'.base_url().'css/styles.css" type="text/css" />'.PHP_EOL.
								'<link rel="stylesheet" href="'.base_url().'css/calendar.css" type="text/css" />'.PHP_EOL.
								'<script src="'.site_url("js/jquery.form.js").'" type="text/javascript"></script>'.PHP_EOL;
		
			$data['pagetype'] = 'nobg';
			$this->load->view('calendar/eventDetailsView',$data);
		}
		else redirect('calendar'); //if not valid redirect to main calendar
	}
	
	function rsvp(){
		$data = $this->_buildContent("rsvp");
		if(isset($data['con_user'])){
			$userGroups = $this->UserModel->getUserAssigns($data['con_user']->userid);
			$event = $this->CalendarModel->getEvent($this->uri->segment(3));
			if($event!=null){
				$data['invited'] = $this->_isInvited($event->category);
		
				if($data['invited']==true){
					$data['event'] = $this->uri->segment(3);
					$this->load->view('calendar/rsvp',$data);
				}
				else{	//can't rsvp to an event you're not invited to
					echo "You're not invited to this event.";
				}
			}
 			else{	//can't rsvp to an invalid event
			
			}
		}
		else{	//can't rvsp if you no a user
		
		}
	}
	
	function dorsvp(){
		$data = $this->_buildContent("rsvp");
		if(isset($data['con_user'])){
			$userGroups = $this->UserModel->getGroupDescriptionsForUser($data['con_user']->userid);
			$event = $this->CalendarModel->getEvent($this->input->post('event'));
			if($event!=null){
				foreach($userGroups as $group){
					if($group==$event->category) $data['invited'] = true;
				}
				if($event->category=='XA') $data['invited'] = true;
		
				if($data['invited']==true){
					$info = array('userid'=>$data['con_user']->userid, 'eventid'=>$this->input->post('event'));
					$data['status'] = $this->CalendarModel->getRSVP($info);
				
					$a = strtolower($this->input->post('submit'));
					$this->session->set_flashdata('msg_info',"You RSVP has been changed to $a");
				
					if($data['status']!=null){ //user wants to change rsvp
						if($this->input->post('submit')=='Attending') $post = array('answer' => 'yes');
						else if($this->input->post('submit')=='Maybe Attending') $post = array('answer' => 'maybe');
						else $post = array('answer' => 'no');
					
						$data['event_id'] = $this->input->post('event');
						$data['user_id'] = $data['con_user']->userid;
					
						$this->load->model('CalendarModel');
						$this->CalendarModel->updateRSVP($data,$post);
						echo "<script type=\"text/javascript\">
						parent.thickboxDone();
						</script>";
					} else{ //user wants to rsvp
						if($this->input->post('submit')=='Attending') $post['answer'] = 'yes';
						else if($this->input->post('submit')=='Maybe Attending') $post['answer'] = 'maybe';
						else $post['answer'] = 'no';
					
						$post['event_id'] = $this->input->post('event');
						$post['user_id'] = $data['con_user']->userid;
					
						$this->load->model('CalendarModel');
						$this->CalendarModel->createRSVP($post);
						echo "<script type=\"text/javascript\">
						parent.thickboxDone();
						</script>";
					
					}
				}
			}
		//can't rsvp to invaild event
			
		}
		//else //redirect
		
		
		
		
		
	}
	
	function map(){
		$data['geocode'] = $this->uri->segment(3) . ',' . $this->uri->segment(4);
		$this->load->view('calendar/map',$data);
	}
	
	//check event id
	function sidebar(){
		$id = $this->uri->segment(3);
		if($id!='none') $build = array('page'=>'sidebar','id'=>$id);
		else $build['page'] = 'none';
		$data = $this->_buildContent($build);
		$this->load->view('calendar/sidebar',$data);
	}
	
	function _validMonth($month){
		if($month!='' && is_numeric($month) && $month>=1 && $month<=12){
			return true;
		}

		return false;
	}
	
	function _validYear($year){
		if($year!='' && is_numeric($year) && strlen($year)==4 && $year>=1970 && $year<=2032){
			return true;
		}

		return false;
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 */
	function _buildContent($temp) {
		if ($this->session->userdata('loggedin')) {
			$admin = $this->UserModel->isUserInGroup($this->UserModel->getUserByName($this->session->userdata('username'))->userid,$this->UserModel->getGroupID('admin'));
			$xalt = $this->UserModel->isUserInGroup($this->UserModel->getUserByName($this->session->userdata('username'))->userid,$this->UserModel->getGroupID('xalt'));
		} else{
			$admin = false;
			$xalt = false;
		}
		//REQUIRED//
		
		/**
		 *Page Type & side bar display
		 */
		if($this->session->userdata('loggedin')){
			if($temp['page']!='createEvent' && $admin!=true) $data['sidebar'] = true;
			elseif($temp['page']=="full" || $temp['page']=='eventDetails') $data['sidebar'] = true;
		}
		if($admin && $temp['page']=='createEvent'){
			$data['pagetype'] = 'page_admin';
		//	$data['admin_nav'] = true;
			$data['extraHead'] = '<link rel="stylesheet" href="'.base_url().'css/admin.css" type="text/css" />';
		}
		if($temp['page']=='fullCalendar'){
			$data['pagetype'] = 'nobg';
		}
		
		
		
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error')) $data['msg_error'] = $this->session->flashdata('msg_error');
		
		//test if user is logged in for site navigation
		if ($this->session->userdata('loggedin')) {
			$data['loggedin'] = TRUE;
			//grab the user
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			//is prayer team?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
				$data['prayer_tab'] = TRUE;
			}

			//is admin?
			if ($admin) {
				$data['admin_tab'] = true;
				
				$data['links'] = '<a href="'.site_url("site/logout").'">logout</a>';
				
				$data['page_info']['title'] = 'calendar';
				
				if(isset($temp['page']) && $temp['page']=="eventDetails"){
					$data['calendar_info']['items'][] = anchor('calendar/create', 'Create a calendar event');
				
					$data['calendar_info']['items'][] = "<a href=\"" . site_url("calendar/editEvent/$temp[id]/") . "\"  title=\"Edit an Event\" id=\"editEvent\">Edit this event</a>";
				
					$data['calendar_info']['items'][] = "<a href=\"" . site_url("calendar/deleteEvent/$temp[id]/") . "\"  title=\"Delete an Event\" id=\"deleteEvent\">Delete this event</a>";
				
					//if($temp['invited']) $data['page_info']['items'][] = "<a href=\"" . site_url("calendar/rsvp/$temp[id]/") . "\" title=\"Change RSVP To\" class=\"thickbox\" id=\"rsvp\">Change RSVP status</a>";
				}
			}			
			//is xalt?
			elseif($xalt) {
				$data['links'] = '<a href="'.site_url("site/logout").'">logout</a>';
				$data['page_info']['title'] = 'calendar';
				$data['page_info']['items'][] = anchor('calendar/create', 'Create a calendar event');
				/*if($temp['page']=="sidebar"){ 
					$data['page_info']['items'][] = "<a href=\"" . site_url("calendar/editEvent/$temp[id]/sidebar/") . "\"  title=\"Edit an Event\" class=\"thickbox\" id=\"editEvent\">Edit this event</a>";
					$data['page_info']['items'][] = "<a href=\"" . site_url("calendar/deleteEvent/$temp[id]/sidebar/") . "\"  title=\"Delete an Event\" class=\"thickbox\" id=\"deleteEvent\">Delete this event</a>";
				}
				else if($temp['page']=='eventDetails'){
					$data['page_info']['items'][] = "<a href=\"" . site_url("calendar/editEvent/$temp[id]/sidebar/") . "\"  title=\"Edit an Event\" class=\"thickbox\" id=\"editEvent\">Edit this event</a>";
					$data['page_info']['items'][] = "<a href=\"" . site_url("calendar/deleteEvent/$temp[id]/sidebar/") . "\"  title=\"Delete an Event\" class=\"thickbox\" id=\"deleteEvent\">Delete this event</a>";
					//$data['page_info']['items'][] = "<a href=\"" . site_url("calendar/createItem/$temp[id]/sidebar/") . "\"  title=\"Add an Item\" class=\"thickbox\" id=\"item\">Add an item to this event</a>";
					if($temp['invited']) $data['page_info']['items'][] = "<a href=\"" . site_url("calendar/rsvp/$temp[id]/") . "\" title=\"Change RSVP To\" class=\"thickbox\" id=\"rsvp\">Change RSVP status</a>";
				}*/
				
			}
			else{  //needs validation that user is in event group
				$data['links'] = '<a href="'.site_url("site/logout").'">logout</a>';
				$data['page_info']['title'] = 'calendar';
				//$data['page_info']['items'][] = anchor("calendar/rsvp/$temp[id]/$user->userid",'Change RSVP status');
			}
			
			$data['con_user'] = $user;
		}
		
		$data['admin'] = $admin;
		$data['xalt'] = $xalt;
		
		return $data;
	}
	
	/*
	 * Admin: Supplies user-specific information to the interface for customization
	 */
	function _buildAdminContent() {
		//REQUIRED//
		$data['pagetype'] = 'page-admin';
		$data['admin_nav'] = TRUE;
		$data['admin'] = true;
		
		//is prayer team?
		if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
			$data['prayer_tab'] = TRUE;
		}
		
		return $data;
	}
}
?>
